//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import java.io.*;
import java.lang.reflect.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Main {
    static final Log log = LogFactory.getLog(Main.class);
    public static void main(String[] args) throws Exception {
        reflectField();
        methodInvoke();
        Generator generator = new Generator();
        generator.getMultipleIds();
//        ForkJoinUtil.executeForkJoinTask();
        String[] names = new String[]{"Jack", "Bob", "Jim", "Bim", "HH", "Keven", "Ke", "JJ", "Ris", "Ren", "Ket"};
        for (int i = 0; i < 5; i++) {
            String name = names[i];
            new Thread(() -> {
                try(var ctx = new UserContext(name)) {
                    ctx.task0();
                    ctx.task1();
                } catch(Exception e) {

                }
            }).start();
        }
        VirtualThreadUtil.testCostTime();
        /*
        String[] arr = asArray("one", "two", "three");
        System.out.println(Arrays.toString(arr));
        // ClassCastException:
        String[] firstTwo = pickTwo("one", "two", "three");
        System.out.println(Arrays.toString(firstTwo));
         */
        QueueHelper.printQueue();

        /*
        DateUtil.getLocalDate();
        DateUtil.getFlightCostTime();
        DateUtil.getLocalDateTimeFromTimeStamp();
         */

        /*
        RegexUtil.isValidPhone();
        RegexUtil.test();
        RegexUtil.pickHMS("23:01:59");

        RegexUtil.countZero();
        System.out.println(Arrays.toString("a, b ;; c".split("[,;\\s]+")));
         */

        /*
        2019年11月20日 08:15
        Nov 19, 2019, 7:15 PM
         */
        System.out.println(DateUtil.getTimeOrder(LocalDateTime.now(), LocalDateTime.parse("2020-10-28T13:28:32"), DateUtil.TimeOrder.isBefore));

        ThreadUtil.executeTask();
    }

    static <K> K[] pickTwo(K k1, K k2, K k3) {
        return asArray(k1, k2);
    }

    static <T> T[] asArray(T... objs) {
        return objs;
    }

    static void reflectField() {
        Person p = new Person("Xiao Ming");
        try {
            Class<? extends Person> c = p.getClass();
            Field f = c.getDeclaredField("name");
            f.setAccessible(true);
            f.set(p, "Xiao Hong");
        } catch (Exception e) {
            log.error("can not find field", e);
        }
    }

    static void methodInvoke() throws Exception {
        String str = "Hello World";
        Method m = String.class.getMethod("substring", int.class, int.class);
        String subStr = (String) m.invoke(str, 0, 5);
        System.out.println(subStr);

        Method m1 = Integer.class.getMethod("parseInt", String.class);
        Integer res = (Integer) m1.invoke(null, "12345");
        System.out.println(res);

        System.out.println(Arrays.toString(Closeable.class.getInterfaces()));

        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                if (method.getName().equals("morning")) {
                    System.out.println(STR."Good morning, \{objects[0]}");
                }
                return null;
            }
        };

        Hello hello = (Hello) Proxy.newProxyInstance(Hello.class.getClassLoader(), new Class[]{ Hello.class }, handler);
        hello.morning("Bob");
    }

    interface Hello {
        void morning(String name);
    }
}

class Score {
    private final int[] scores;
    public Score(int[] scores) {
        this.scores = Arrays.copyOf(scores, scores.length);
    }

    public void printScores() {
        System.out.println(Arrays.toString(scores));
    }
}